package com.example.mediaservice;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LyricManager {
    private static final String TAG = "LyricManager";
    private List<LyricLine> lyrics;
    private OnLyricUpdateListener listener;

    public interface OnLyricUpdateListener {
        void onLyricUpdated(int activeLineIndex, List<LyricLine> lyrics);
    }

    public static class LyricLine {
        public long time; // 毫秒
        public String text;

        public LyricLine(long time, String text) {
            this.time = time;
            this.text = text;
        }
    }

    public LyricManager() {
        lyrics = new ArrayList<>();
    }

    public void setOnLyricUpdateListener(OnLyricUpdateListener listener) {
        this.listener = listener;
    }

    /**
     * 解析LRC格式歌词
     * @param lyricText LRC歌词文本
     */
    public void parseLyrics(String lyricText) {
        lyrics.clear();

        if (lyricText == null || lyricText.isEmpty()) {
            notifyLyricUpdate();
            return;
        }

        String[] lines = lyricText.split("\n");
        Pattern timePattern = Pattern.compile("\\[(\\d{2}):(\\d{2})\\.(\\d{2,3})\\]");

        for (String line : lines) {
            Matcher matcher = timePattern.matcher(line);
            String text = line;

            // 移除所有时间标签，获取纯文本
            while (matcher.find()) {
                text = line.substring(matcher.end());
            }

            // 重新匹配以获取时间
            matcher = timePattern.matcher(line);
            while (matcher.find()) {
                int minutes = Integer.parseInt(matcher.group(1));
                int seconds = Integer.parseInt(matcher.group(2));
                int milliseconds = Integer.parseInt(matcher.group(3));

                // 如果毫秒是两位数，需要乘以10
                if (matcher.group(3).length() == 2) {
                    milliseconds *= 10;
                }

                long time = minutes * 60 * 1000 + seconds * 1000 + milliseconds;
                lyrics.add(new LyricLine(time, text));
            }
        }

        // 按时间排序
        Collections.sort(lyrics, (l1, l2) -> Long.compare(l1.time, l2.time));
        notifyLyricUpdate();
    }

    /**
     * 根据当前播放时间更新歌词高亮
     * @param currentTime 当前播放时间(毫秒)
     */
    public void updateLyric(long currentTime) {
        if (lyrics.isEmpty() || listener == null) return;

        int activeIndex = -1;
        for (int i = 0; i < lyrics.size(); i++) {
            if (lyrics.get(i).time <= currentTime) {
                activeIndex = i;
            } else {
                break;
            }
        }

        listener.onLyricUpdated(activeIndex, lyrics);
    }

    /**
     * 清除歌词
     */
    public void clearLyrics() {
        lyrics.clear();
        notifyLyricUpdate();
    }

    private void notifyLyricUpdate() {
        if (listener != null) {
            listener.onLyricUpdated(-1, lyrics);
        }
    }
}

